package com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.BlankSubjectFragment;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.ChooseSubjectFragment;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.ExamResultFragment;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.JudgmentProblemFragment;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.QuestionAndAnswerFragment;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.SubjectTypeUtil;
import com.dayaokeji.server_api.domain.Exam;
import com.dayaokeji.server_api.domain.ExamQuestion;

import java.util.List;

/**
 * Created by Gorden on 2017/10/28.
 */

public class AnswerViewpagerAdapter extends FragmentPagerAdapter {

    List<ExamQuestion> examQuestions;
    private boolean isExamResult = false;
    private Exam exam;

    public AnswerViewpagerAdapter(FragmentManager fm, List<ExamQuestion> examQuestions, boolean isExamResult, Exam exam) {
        super(fm);
        this.examQuestions = examQuestions;
        this.isExamResult = isExamResult;
        this.exam = exam;
    }

    @Override
    public Fragment getItem(int position) {
        if (isExamResult) {
            if (position == 0) {
                return ExamResultFragment.newInstance(exam.getActScore(),exam.getTotalScore());
            }else{
                ExamQuestion examQuestion = examQuestions.get(position);
                return subjectFragmentByType(examQuestion);
            }
        }
        ExamQuestion examQuestion = examQuestions.get(position);
        return subjectFragmentByType(examQuestion);
    }

    @Override
    public int getCount() {
        return examQuestions.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (isExamResult) {
            if (position == 0) {
                return "成绩";
            }else{
                return "第"+(position)+"题";
            }
        }
        return "第"+(position+1)+"题";
    }

    private Fragment subjectFragmentByType(ExamQuestion examQuestion) {
        final int type = examQuestion.getType();
        switch (type) {
            case SubjectTypeUtil.SINGLE_CHOOSE:
                return ChooseSubjectFragment.newInstance(examQuestion).setShowExamResult(isExamResult);
                case SubjectTypeUtil.MULTIPLE_CHOOSE:
                    return ChooseSubjectFragment.newInstance(examQuestion).setShowExamResult(isExamResult);
                    case SubjectTypeUtil.QUESTION_AND_ANSWER:
                        return QuestionAndAnswerFragment.newInstance(examQuestion).setShowExamResult(isExamResult);
                        case SubjectTypeUtil.BLANK_SUBJECT:
                            return BlankSubjectFragment.newInstance(examQuestion).setShowExamResult(isExamResult);
                            case SubjectTypeUtil.JUDGMENT_PROBLEM:
                                return JudgmentProblemFragment.newInstance(examQuestion).setShowExamResult(isExamResult);
                                default:
                                    return null;
        }
    }
}
